package cn.com.api.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiU_discover_cate;
import cn.com.api.service.ApiU_discover_cateService;
import cn.com.api.service.CheckService;
import cn.com.util.*;

/**
 * 新发现类别
 */

@Controller
@Qualifier("cn.com.api.service.impl.ApiU_discoverServiceImpl")
@RequestMapping(Utils.SYS_API + "discovercate/")
public class ApiU_discover_cateController {
	@Autowired
	private ApiU_discover_cateService apiU_discover_cateService;
	@Autowired
	private CheckService checkService;

	private static Logger logger = Logger.getLogger(ApiU_discover_cateController.class);

	/**
	 * 获取所有新发现类别
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findDiscoverCatesList")
	public void findDiscoverCatesList(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {

			List<ApiU_discover_cate> u_discover_cates = apiU_discover_cateService.findAllU_discover();

			map.put("datas", u_discover_cates);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");

		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 热搜列表
	 * 
	 * @param request
	 * @param response
	 * @param type
	 * @throws Exception
	 */
	@RequestMapping("/findDiscoverCatesListByType")
	public void findDiscoverCatesListByType(HttpServletRequest request, HttpServletResponse response, String cate_type)
			throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<ApiU_discover_cate> u_discover_cates = apiU_discover_cateService
					.findU_discover_byType(Integer.valueOf(cate_type));
			map.put("datas", u_discover_cates);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 根据传入类别的名称获取所有新发现
	 * 
	 * @param catenames
	 *            已选新发现的名称集合，多个用:分隔
	 * @param type
	 *            新发现分类的类型；0-活动分类；1-服务于需求的分类
	 * @param response
	 * @return
	 */
	@RequestMapping("/getDiscoverCatesByStr")
	public void getDiscoverCatesByStr(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String catenames, String type) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				if (catenames != null && !"".equals(catenames)) {
					map.put("catenames", catenames.replace(":", ","));
				}
				if (type != null && !"".equals(type)) {
					map.put("type", type.replace(":", ","));
				}
				List<ApiU_discover_cate> u_discover_cates = apiU_discover_cateService.findU_discover_catesByMap(map);
				map.clear();
				map.put("datas", u_discover_cates);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "查询成功！");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	@RequestMapping("/findDiscoverFavoriteList")
	public void findDiscoverFavoriteList(HttpServletRequest request, HttpServletResponse response, String user_id)
			throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> param = new HashMap<String, Object>();
		try {
			List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
			List<Map<String, Object>> u_discover_info = apiU_discover_cateService.findDiscoverFavoriteList(user_id);
			for (Map<String, Object> map2 : u_discover_info) {
				param = new HashMap<String, Object>();
				if (map2.get("TYPE") != null && !map2.get("TYPE").equals("")) {
					param.put("favorite_count", map2.get("FAVORITE_COUNT") + "");
					param.put("type", map2.get("TYPE") + "");
					result.add(param);
				}
			}

			map.put("datas", result);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");

		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

}
